package com.morn.weibo.fragment;

import android.app.DialogFragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.morn.weibo.R;
import com.morn.weibo.WeiboApplication;
import com.morn.weibo.http.Fanfou;
import com.morn.weibo.http.image.Bitmap4View;

public class ShowImageFragment extends DialogFragment {

	private String imageUrl;
	private Fanfou fanfou;
	private Bitmap4View bitmapTask;
	private ImageView ivImage;
	private TextView tvImageDownload;
	private ProgressBar pbImageDownload;

	public static ShowImageFragment newInstance(String imageUrl) {
		ShowImageFragment frag = new ShowImageFragment();
		Bundle args = new Bundle();
		args.putString("imageUrl", imageUrl);
		frag.setArguments(args);
		return frag;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fanfou = WeiboApplication.getFanfou();
		bitmapTask = new Bitmap4View(getActivity(), fanfou.getHttpClient());
		setStyle(DialogFragment.STYLE_NO_FRAME,
				android.R.style.Theme_Holo_Light_Dialog);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		imageUrl = getArguments().getString("imageUrl");
		View rootView = inflater.inflate(R.layout.view_dialog_show_image,
				container, false);
		ivImage = (ImageView) rootView.findViewById(R.id.ivImage);
		tvImageDownload = (TextView) rootView
				.findViewById(R.id.tvImageDownload);
		pbImageDownload = (ProgressBar) rootView
				.findViewById(R.id.pbImageDownload);
		return rootView;
	}

	@Override
	public void onStart() {
		super.onStart();
		bitmapTask.execute(imageUrl, ivImage, tvImageDownload, pbImageDownload,
				true);
	}

	@Override
	public void onStop() {
		super.onStop();
		if (bitmapTask != null
				&& bitmapTask.getStatus() == AsyncTask.Status.RUNNING) {
			bitmapTask.cancel(true);
		}
	}

}
